Installation

Purpose: set up a fresh computer to be able to run all aspects of your model and analysis.

SetupTools:

  • Makes your code importable using Python
  • Checks all requirements are satisified
  • Makes your code executable in the shell regardless of the OS

Bonus features not covered in this talk:

  • Keeping track of your versions
  • Uploading stuff to PyPi

setup.py

Packaging, getting stuff to run reliably on various platforms, is hard, but SetupTools tries to make it easier in Python. Learning to use SetupTools starts with learning to use the setup.py file. If you have pip installed, then you have SetupTools installed.

Making your code importable

Let's say you're trying to make a package called bugkiller and you want to be able run import bugkiller from other Python programs. Assume your folder structure looks like:

|- README.md
|- killer
|   |-- __init__.py
|   |-- kill.py
|   | -- tests
|   |   |-- test_kill.py

Wherein 'kill.py' contains two functions: spray() and shoot().

And the root folder is named something like bughandler-project. The first thing you need to do is make a setup.py file in the root directory with the contents indicated below and run python setup.py develop:


In [ ]:
from setuptools import setup

setup(
    name='bughandler',
    packages=['killer']
)

Now when you run import killer you won't get an error. But what if killer isn't your only package? What if you want to add lover later? Isn't defining all the packages going to be tiresome? That's where find_packages comes in. Try this code in your setup.py file:


In [ ]:
from setuptools import setup, find_packages

setup(
    name='bughandler',
    packages=find_packages()
)

This should have the same effect. Obviously, if you really want your import statement to do something useful, you're going to need to add some content to kill.py and access it via import killer.kill.

Note: Usually your name and your root package have the same string (for example, in the Nengo repository), but I avoided that case here to reduce confusion.

Requirements

Let's say bughandler depends the PyPi package six any version after 1.9. How can you make sure that the user has that package installed in the least instrusive way? SetupTools has you covered with the install_requires argument to the setup function:


In [ ]:
from setuptools import setup, find_packages

setup(
    name='bughandler',
    packages=find_packages(),
    install_requires=[
        "six>=1.9",
    ],
)

Now if the user doesn't have the right version of six installed, it should be installed for them via PyPi when they run python setup.py develop.

Script/Command Generation

Sometimes you have a standalone application that you want a user to be able to use from their commandline. Regardless of the OS, SetupTools has got you covered here too with the entry_points argument.

Although entry_points can also be used to extend your application with plugins, this section will only focus on creating shell scripts, like the Nengo GUI, with another argument to setup().


In [ ]:
from setuptools import setup, find_packages

setup(
    name='bughandler',
    packages=find_packages(),
    entry_points={
        'console_scripts': [
            'spray_bug = killer.kill:spray',
            'shoot_bug = killer.kill:shoot',
        ]
    }
)

The final product of all this hard work can be found on Seanny123/bugkiller repository.